﻿package com.evidencer.java.net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.charset.Charset;

public class UDPChatServer implements Runnable {

	@Override
	public void run() {
		int port=9999;
		System.out.println("开始聊天：");
		try{
			// 初始化监听套接字
			DatagramSocket dsReceive=new DatagramSocket(port);
			while(true){
				//接收消息
				DatagramPacket dp=new DatagramPacket(new byte[1024],1024);
				dsReceive.receive(dp);
				String receiveStr=new String(dp.getData(),Charset.forName("GBK"));
				// 打印消息
				System.out.println(dp.getAddress().getHostAddress()+"say:"+receiveStr.trim());
			}
		}catch(Exception e){}
	}

}
